package com.irdstudio.efp.nls.service.impl;

import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.efp.nls.service.dao.NlsApplyRelationDao;
import com.irdstudio.efp.nls.service.domain.NlsApplyRelation;
import com.irdstudio.efp.nls.service.facade.NlsApplyRelationService;
import com.irdstudio.efp.nls.service.vo.NlsApplyRelationVO;

/**
 * 网贷申请联系人信息
 * 
 * @author heyingfeng
 * @since 2018年12月17日 下午7:46:32
 * @version 1.0
 */
@Service("nlsApplyRelationService")
public class NlsApplyRelationServiceImpl implements NlsApplyRelationService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(NlsApplyRelationServiceImpl.class);

	@Autowired
	private NlsApplyRelationDao nlsApplyRelationDao;

	/**
	 * 新增操作
	 * 
	 * @throws Exception
	 */
	@Override
	public int insertNlsApplyRelation(NlsApplyRelationVO inNlsApplyRelationVo) throws Exception {
		logger.debug("当前新增数据为:" + inNlsApplyRelationVo.toString());
		int num = 0;
		try {
			NlsApplyRelation nlsApplyRelation = new NlsApplyRelation();
			beanCopy(inNlsApplyRelationVo, nlsApplyRelation);
			num = nlsApplyRelationDao.insertNlsApplyRelation(nlsApplyRelation);
		} catch (DuplicateKeyException dke) {
			throw new Exception("新增数据发生异常，主键冲突!");
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
			throw e;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 删除操作
	 */
	@Override
	public int deleteByPk(NlsApplyRelationVO inNlsApplyRelationVo) {
		logger.debug("当前删除数据条件为:" + inNlsApplyRelationVo);
		int num = 0;
		try {
			NlsApplyRelation nlsApplyRelation = new NlsApplyRelation();
			beanCopy(inNlsApplyRelationVo, nlsApplyRelation);
			num = nlsApplyRelationDao.deleteByPk(nlsApplyRelation);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inNlsApplyRelationVo + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 更新操作
	 */
	@Override
	public int updateByPk(NlsApplyRelationVO inNlsApplyRelationVo) {
		logger.debug("当前修改数据为:" + inNlsApplyRelationVo.toString());
		int num = 0;
		try {
			NlsApplyRelation nlsApplyRelation = new NlsApplyRelation();
			beanCopy(inNlsApplyRelationVo, nlsApplyRelation);
			num = nlsApplyRelationDao.updateByPk(nlsApplyRelation);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inNlsApplyRelationVo + "修改的数据条数为" + num);
		return num;
	}

	/**
	 * 查询操作
	 */
	@Override
	public NlsApplyRelationVO queryByPk(NlsApplyRelationVO inNlsApplyRelationVo) {

		logger.debug("当前查询参数信息为:" + inNlsApplyRelationVo);
		try {
			NlsApplyRelation queryNlsApplyRelation = new NlsApplyRelation();
			beanCopy(inNlsApplyRelationVo, queryNlsApplyRelation);
			NlsApplyRelation queryRslNlsApplyRelation = nlsApplyRelationDao.queryByPk(queryNlsApplyRelation);
			if (Objects.nonNull(queryRslNlsApplyRelation)) {
				NlsApplyRelationVO outNlsApplyRelationVo = beanCopy(queryRslNlsApplyRelation, new NlsApplyRelationVO());
				logger.debug("当前查询结果为:" + outNlsApplyRelationVo.toString());
				return outNlsApplyRelationVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 根据申请流水号查询查询
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<NlsApplyRelationVO> queryByApplySeq(NlsApplyRelationVO nlsApplyRelationVo) {
		logger.debug("当前查询本人所属数据信息的参数信息为:" + nlsApplyRelationVo);
		try {
			NlsApplyRelation queryNlsApplyRelation = new NlsApplyRelation();
			beanCopy(nlsApplyRelationVo, queryNlsApplyRelation);
			List<NlsApplyRelation> queryRslNlsApplyRelation = nlsApplyRelationDao.queryByApplySeq(queryNlsApplyRelation);
			if (Objects.nonNull(queryRslNlsApplyRelation)) {
				List<NlsApplyRelationVO> queryRslNlsApplyRelationVO = (List<NlsApplyRelationVO>) beansCopy(queryRslNlsApplyRelation,
						NlsApplyRelationVO.class);
				logger.debug("当前查询结果为:" + queryRslNlsApplyRelationVO.toString());
				return queryRslNlsApplyRelationVO;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询用户权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<NlsApplyRelationVO> queryAllOwner(NlsApplyRelationVO nlsApplyRelationVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<NlsApplyRelationVO> list = null;
		try {
			List<NlsApplyRelation> nlsApplyRelations = nlsApplyRelationDao.queryAllOwnerByPage(nlsApplyRelationVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + nlsApplyRelations.size());
			pageSet(nlsApplyRelations, nlsApplyRelationVo);
			list = (List<NlsApplyRelationVO>) beansCopy(nlsApplyRelations, NlsApplyRelationVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<NlsApplyRelationVO> queryAllCurrOrg(NlsApplyRelationVO nlsApplyRelationVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<NlsApplyRelation> nlsApplyRelations = nlsApplyRelationDao.queryAllCurrOrgByPage(nlsApplyRelationVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + nlsApplyRelations.size());
		List<NlsApplyRelationVO> list = null;
		try {
			pageSet(nlsApplyRelations, nlsApplyRelationVo);
			list = (List<NlsApplyRelationVO>) beansCopy(nlsApplyRelations, NlsApplyRelationVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<NlsApplyRelationVO> queryAllCurrDownOrg(NlsApplyRelationVO nlsApplyRelationVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<NlsApplyRelation> nlsApplyRelations = nlsApplyRelationDao.queryAllCurrDownOrgByPage(nlsApplyRelationVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + nlsApplyRelations.size());
		List<NlsApplyRelationVO> list = null;
		try {
			pageSet(nlsApplyRelations, nlsApplyRelationVo);
			list = (List<NlsApplyRelationVO>) beansCopy(nlsApplyRelations, NlsApplyRelationVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 关联联系人查询
	 * 
	 * @param page
	 * @param size
	 * @return
	 */
	@Override
	public List<NlsApplyRelationVO> queryNlsApplyRelationRelAll(NlsApplyRelationVO nlsApplyRelationVo) {
		logger.debug("当前查询关联联系人数据信息的参数信息为:" + nlsApplyRelationVo);
		List<NlsApplyRelation> nlsApplyRelations = nlsApplyRelationDao.queryNlsApplyRelationRelByPage(nlsApplyRelationVo);
		logger.debug("当前查询关联联系人数据信息的结果集数量为:" + nlsApplyRelations.size());
		List<NlsApplyRelationVO> list = null;
		try {
			pageSet(nlsApplyRelations, nlsApplyRelationVo);
			list = (List<NlsApplyRelationVO>) beansCopy(nlsApplyRelations, NlsApplyRelationVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

}
